package com.example.home.adapter;


import android.content.Context;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.home.activity.R;
import com.example.home.viewmodel.home.HotGoodsList;
import com.example.home.viewmodel.home.NewGoodsList;

import java.util.List;

public class HotGoodsAdapter extends BaseAdapter {

    private Context ctx;
    private List<HotGoodsList> hotGoodsList;

    public HotGoodsAdapter(Context ctx, List<HotGoodsList> hotGoodsList) {
        this.ctx = ctx;
        this.hotGoodsList = hotGoodsList;
    }

    @Override
    public int getCount() {
        return this.hotGoodsList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.hotGoodsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return this.hotGoodsList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView==null){
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(this.ctx).inflate(R.layout.home_hot_item,null);
            viewHolder.home_hot_img = convertView.findViewById(R.id.home_hot_img);
            viewHolder.home_hot_name = convertView.findViewById(R.id.home_hot_name);
            viewHolder.home_hot_brief = convertView.findViewById(R.id.home_hot_brief);
            viewHolder.home_hot_price_count = convertView.findViewById(R.id.home_hot_price_count);
            viewHolder.home_hot_price_retail = convertView.findViewById(R.id.home_hot_price_retail);
            convertView.setTag(viewHolder);
        }else{
            viewHolder = (ViewHolder) convertView.getTag();
        }
        Glide.with(convertView.getContext())
                .load(this.hotGoodsList.get(position).getPicUrl())
                .into(viewHolder.home_hot_img);
        viewHolder.home_hot_name.setText(this.hotGoodsList.get(position).getName());
        viewHolder.home_hot_brief.setText(this.hotGoodsList.get(position).getBrief());
        viewHolder.home_hot_price_count.setText("￥"+this.hotGoodsList.get(position).getCounterPrice().toString());
        viewHolder.home_hot_price_retail.setText("￥"+this.hotGoodsList.get(position).getRetailPrice().toString());
        viewHolder.home_hot_price_retail.setPaintFlags(viewHolder.home_hot_price_retail.getPaintFlags() & (~Paint.STRIKE_THRU_TEXT_FLAG));
        return convertView;
    }

    static class ViewHolder {
        ImageView home_hot_img;
        TextView home_hot_name;
        TextView home_hot_brief;
        TextView home_hot_price_count;
        TextView home_hot_price_retail;
    }
}

